package com.erau.droidsatframework.core;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DataAccessLayer {
	
	private Context context;
	private SQLiteDatabase db;
	private LoggingOpenHelper logHelp;
	
	private long runID;
	
	public DataAccessLayer(Context context){
		this.context = context;
	}
	
	public DataAccessLayer open() throws SQLException {
		runID = System.currentTimeMillis();
		logHelp = new LoggingOpenHelper(context);
		db = logHelp.getWritableDatabase();
		return this;
		
	}
	
	public void close() {
		logHelp.close();
	}
	
	public void logADC (long timestamp, double dAngle, double angle, double velocity){
		String ADC_INSERT = "INSERT INTO ADC (run_id, timestamp, dAngle, Angle, Velocity) VALUES("+runID+","+timestamp+","+dAngle+","+angle+","+velocity+");";
		db.execSQL(ADC_INSERT);
	}
	
	public void logPower (long timestamp, int level, int voltage, int temp){
		String POWER_INSERT = "INSERT INTO Power (run_id, timestamp, level, voltage, temperature) VALUES("+runID+","+timestamp+","+level+","+voltage+","+temp+");";
		db.execSQL(POWER_INSERT);
	}
	
	public void logGeneral(String tag, String msg){
		String GENERAL_INSERT = "INSERT INTO General (run_id, timestamp, tag, msg) VALUES ("+runID+","+System.currentTimeMillis()+","+tag+","+msg+");";
		db.execSQL(GENERAL_INSERT);
	}

}
